﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace AudioSourceManager
{
    internal class VolumeMonitor
    {
        private readonly string _channelName;
        private Timer _volumeTimer;
        private float _currentMaxVolume;
        private readonly object _volumeLock = new object();

        public VolumeMonitor(string channelName)
        {
            _channelName = channelName;
        }

        public void StartMonitoring()
        {
            _volumeTimer = new Timer(UpdateVolume, null, 0, 40); // 40ms = 25次/秒
        }

        private void UpdateVolume(object state)
        {
            float currentVolume;
            lock (_volumeLock)
            {
                currentVolume = _currentMaxVolume;
                _currentMaxVolume = 0;
            }
            Console.WriteLine($"{_channelName}音量: {currentVolume:P}");
        }

        public void UpdateMaxVolume(float newVolume)
        {
            lock (_volumeLock)
            {
                if (newVolume > _currentMaxVolume)
                {
                    _currentMaxVolume = newVolume;
                }
            }
        }
    }
}
